package main;

import java.util.*;

import draw.StdDraw;

import file_import.In;

public class Brute {

    public static Set<Point> pointsList;
    public static Set<Point[]> collinearPointsList = new HashSet<Point[]>();

    public static void main(String[] args) {
    	long startTime = System.currentTimeMillis();
        StdDraw.setXscale(0, 32768);
        StdDraw.setYscale(0, 32768);
        Brute brute = new Brute();
//        brute.readPointsFromFile(args[0]);
        brute.readPointsFromFile("collinear/grid4x4_a.txt");
//        brute.readPointsFromFile("collinear/input100.txt");
        brute.runCheck1();
        StdDraw.show(0);
        System.out.println(collinearPointsList.size());
        long endTime = System.currentTimeMillis();
        System.out.println("Time: " + (endTime - startTime)/1000 + " seconds");
    }

    public Brute() {
        //
    }
    
    public void runCheck1() {
    	int i = 0;
    	for (Point p : pointsList) {
    		for (Point q : pointsList) {
    			for (Point r : pointsList) {
    				for (Point s : pointsList) {
    					i++;
    					if (pointsOK(p,q,r,s)) {
	    					double slope = p.slopeTo(q);
	    					double slope1 = q.slopeTo(r);
	    					double slope2 = r.slopeTo(s);
        					if (slope == slope1 && slope == slope2) {
        						boolean distinct = true;
        						Point points[] = new Point[4];
        						points[0] = p;
        						points[1] = q;
        						points[2] = r;
        						points[3] = s;
        						Arrays.sort(points);
        						checkDistinct: for (Point[] p1 : collinearPointsList) {
        							if (p1[0].equals(points[0]) && p1[1].equals(points[1]) && p1[2].equals(points[2]) && p1[3].equals(points[3])){
        								distinct = false;
        								break checkDistinct;
        							}        								
        						}
        						if (distinct){
        							collinearPointsList.add(points);
        							printPoints(points);
        						}
        						p.drawTo(s);
        					}	
    					}
    				}
    			}
    		}
    	}
    	System.out.println(i);
    }
    
    private static boolean pointsOK(Point p, Point q, Point r, Point s) {
    	if (p.equals(q)  || p.equals(r) || p.equals(s))
    		return false;
    	if (q.equals(r) || q.equals(s))
    		return false;
    	if (r.equals(s))
    		return false;
    	return true;
    }
    
    private static void printPoints(Point[] p) {
    	//for (int i = 0; i < p.length; i++)
    	System.out.print(p[0]+" -> "+p[1]+" -> "+p[2]+" -> "+p[3]);
    	System.out.println();
    }
    
    public void checkForCollinearityAgainstOtherPoints(Point a) {
    	ArrayList<Point> tmp = new ArrayList<Point>();
    	for (Point p : pointsList) {
    		double baseSlope = a.slopeTo(p);
    		tmp.add(a);
    		tmp.add(p);
    		int i = 0;
    		for (Point q : pointsList) {
    			double testSlope = a.slopeTo(q);
    			if (baseSlope == testSlope && testSlope != Double.NEGATIVE_INFINITY) {
//    				System.out.println("p.x: "+p.x+" p.y: "+p.y+" q.x: "+q.x+" q.y: "+q.y);
//    				System.out.println("baseSlope: "+baseSlope+"  testSlope: "+testSlope);
//    				System.out.println("i: "+i);
    				tmp.add(q);
    				if (i > 2){
    					//collinearPointsList.add(tmp);
    					//a.drawTo(q);
    					//System.out.println("drawing from: "+a.toString()+" to: "+q.toString());
    					System.out.println("adding tmp");
    				}
    				i++;
    			}
    		}
    	}
    }
    
    public boolean checkCollinear(Point p, Point p1, Point p2) {
        double slope = p.slopeTo(p1);
        double slope1 = p.slopeTo(p2);
        if (slope == slope1)
            return true;
        return false;
    }

    public void readPointsFromFile(String filename) {
        pointsList = new HashSet<Point>();
        String f = filename;
        In in = new In(f);
        int N = in.readInt();
        for (int i = 0; i < N; i++) {
            int x = in.readInt();
            int y = in.readInt();
            Point p = new Point(x, y);
            pointsList.add(p);
            p.draw();
        }
    }
} 
